/**
 * cdragon.cpp
 * Copyright (C) 2009 Hajime Yoshimori <RoarLudros@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// dragon script headers
#include "cdragon.h"
#include <DragonInit>
#include <DragonExcept>
#include <DragonMessage>
#include <DragonPrintf>

#include <DragonInt>

#include "config.h"

// standard headers
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;
using namespace dragon;
using dragon::sprintf;

//-<public>--------------------------------------------------------------------
cdragon::cdragonapp::cdragonapp(int argc, char** argv) {
  // initial setup.
  interactive = false;
  debug = false;
  warning = false;
  
  
  // parameter parser.
  bool no_parse = false;
  bool next_param_arg = false;
  for(int i = 0; i < argc; i++) {
    if(argv[i][0] == '-' && (!no_parse)) {
	  if(next_param_arg) {
		throw DragonExcept(&msgParRequired, std::string(argv[i]));
	  }
      if(argv[i][1] == '-') {
		if(strcmp(&(argv[i][2]), "") == 0) {
		  no_parse = true;
		} else if(strcmp(&(argv[i][2]), "help") == 0) {
		  view_help();
		  exit(037);
		} else if(strcmp(&(argv[i][2]), "interactive") == 0) {
		  interactive = true;
		} else if(strcmp(&(argv[i][2]), "debug") == 0) {
		  debug = true;
		} else if(strcmp(&(argv[i][2]), "version") == 0) {
		  view_version();
		  exit(037);
		} else if(strcmp(&(argv[i][2]), "license") == 0) {
		  view_license();
		  exit(037);
		} else {
		  throw dragon::DragonExcept(&dragon::msgUnknownOption, std::string(argv[i]));
		}
	  } else {
		for(unsigned int j = 1; j < strlen(argv[i]); j++) {
		  switch(argv[i][j]) {
			case 'h':
			  view_help();
			  exit(037);
			  break;
			case 'V':
			  view_version();
			  exit(037);
			  break;
			case 'i':
			  interactive = true;
			  break;
			case 'd':
			  debug = true;
			  break;
			case 'W':
			  warning = true;
			  break;
			default:
			  throw dragon::DragonExcept(&dragon::msgUnknownOption, std::string(argv[i]));
		  }
		}
	  }
    } else {
      
    }
  }
}

void cdragon::cdragonapp::init(int argc, char** argv) {
  cdragonapp(argc,argv);
}

int cdragon::cdragonapp::exec() {
  try {
	if(interactive) {
	  view_hello();
	}
	
  } catch(bad_alloc) {
	throw dragon::DragonExcept(&dragon::msgMemoryIsFull);
  } catch(dragon::DragonExcept& ex) {
	throw ex;
  }
  return 0;
}

//-<private>-------------------------------------------------------------------
void cdragon::cdragonapp::view_help() {
  cout << _(
	"How to call: cdragon [options] [script file]\n"
	"\n"
	"[options]:\n"
	" -h --help            : View this help and exit.\n"
	" -i --interactive     : Interactive mode.\n"
	" -d --debug           : Script Debugging mode.\n"
	" -c --code=[name]     : Specify charactor coding of script.\n"
	" -l --language=[name] : Specify language of script.\n"
	" -W --warn-all        : Enable all warning.\n"
	" --teach              : Teaching mode.\n"
	" --system-debug       : CDragon Debugging mode.\n"
	" --license            : View license informaition and exit.\n"
	" -V --version         : View version informaition and exit.\n"
  );
}

void cdragon::cdragonapp::view_license() {
  /* TRANSLATORS: You can translate this. But I think you should not translate
     if you want to keep the restriction of the license. */
  cout << _(
	"Dragon Script is free software: you can redistribute it and/or modify\n"
	"it under the terms of the GNU General Public License as published by\n"
	"the Free Software Foundation, either version 3 of the License, or\n"
	"(at your option) any later version.\n"
	"\n"
	"This program is distributed in the hope that it will be useful,\n"
	"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
	"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
	"GNU General Public License for more details.\n"
	"\n"
	"You should have received a copy of the GNU General Public License\n"
	"along with this program.  If not, see <http://www.gnu.org/licenses/>.\n"
  );
}

void cdragon::cdragonapp::view_version() {
  cout << DS_VERSION << endl;
}

void cdragon::cdragonapp::view_hello() {
  cout << dragon::sprintf(_(
	"Welcome to the Dragons\' World! I\'m version %s of \"C\"Dragon.\n"
	"Copyright (C) 2010 Dragon Script Developing Team\n"
	"\n"
	"This program is licensed under GPL of version 3, and comes with No Warranty.\n"
	"You can redistribute under certain conditions.\n"
	"If you want to get License Information, command \"Show (the) License.\"\n"
	"If you want to get about Redistribution, command \"Show (the) Redist(ribution).\"\n"
	"\n"
	"If you don\'t have any idea about commanding us, use \"teach\" mode."
	"\n"
	"Please wait for my reading books.\n"
  ).c_str(), DS_VERSION);
}
